﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Unit : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string unit_id;

        public frm_Edit_Unit()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.unit_id = null;
        }

        //////////////////////////
        /// Load data to form
        //////////////////////////
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(unit_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Unit] WHERE UnitID='" + unit_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.unitID.Text = dataReader["UnitID"].ToString();
                        this.englishName.Text = dataReader["UnitNameEN"].ToString();
                        this.vietnamName.Text = dataReader["UnitNameVN"].ToString();
                    }
                    else
                    {
                        ret = false;
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;            
            string english_name = null;            
            string vietnamese_name = null;
                        
            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.englishName.Text))
                {
                    error = "Please enter English Name";
                }
                else
                {
                    english_name = this.englishName.Text;
                }
            }
            
            if (string.IsNullOrEmpty(error))
            {
                vietnamese_name = this.vietnamName.Text;
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Unit] SET UnitNameEN=N'" + english_name + "', UnitNameVN=N'" + vietnamese_name + "'"
                        + " WHERE UnitID='" + unit_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();                    
                    MessageBox.Show("Unit updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Unit. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }        
    }
}
